package com.ysh.service.impl;

import com.github.pagehelper.PageHelper;
import com.ysh.domain.Student;
import com.ysh.domain.User;
import com.ysh.mapper.StudentMapper;
import com.ysh.mapper.UserMapper;
import com.ysh.service.StudentService;
import com.ysh.service.UserService;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.util.List;

/*
    业务层实现类：通过代理接口方式
 */
public class StudentServiceImpl implements StudentService {
    // 创建持久层对象：动态代理来获取UserMapper代理对象
    private static StudentMapper stuMapper = null;
    // 静态代码块，加载必要内容
    static{
        try {
            // 构建工厂接口对象
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("MyBatisConfig.xml"));
            // 获取SqlSession对象
            SqlSession sqlSession = sqlSessionFactory.openSession(true);
            // 接口代理方式
            stuMapper = sqlSession.getMapper(StudentMapper.class);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /*
        查询所有学生信息
     */
    @Override
    public List<Student> findAll(Integer nowPage) {
        // 分页条件
        PageHelper.startPage(nowPage,3);
        return stuMapper.findAll();
    }
}
